package com.fullertontech.framework.dubbo;

import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ConsumerConfig;
import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.ProviderConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.util.StringUtils;


@Import(DubboScanConfig.class)
public class DubboConfig {
	
	@Value("${dubbo.application.name}")
	private String applicationName;
	@Value("${dubbo.address}")
	private String address;
	@Value("${dubbo.protocol.name}")
	private String protocolName;
	@Value("${dubbo.protocol.port}")
	private Integer protocolPort;
	@Value("${dubbo.provider.accesslog}")
	private String accesslog;
	@Value("${dubbo.provider.filter:provideFilter}")
	private String provideFilter;
	@Value("${dubbo.provider.timeout:5000}")
	private Integer timeout;
	
	@Bean
	public ApplicationConfig productApplicationConfig() {
		ApplicationConfig config=new ApplicationConfig(applicationName);
		return config;
	}
	@Bean
	public RegistryConfig productRegistryConfig() {
		RegistryConfig registryConfig=new RegistryConfig();
		registryConfig.setAddress(address);
		return registryConfig;
	}
	@Bean
	public ProtocolConfig productProtocolConfig() {
		ProtocolConfig protocolConfig=new ProtocolConfig();
		protocolConfig.setName(protocolName);
		protocolConfig.setPort(protocolPort);
		return protocolConfig;
	}
	
	@Bean
	public ProviderConfig productProviderConfig() {
		ProviderConfig providerConfig=new ProviderConfig();
		providerConfig.setTimeout(timeout);
		providerConfig.setRetries(0);
		providerConfig.setThreadpool("fixed");
		providerConfig.setThreads(200);
		providerConfig.setAccepts(1000);
		providerConfig.setPayload(30000000);
		providerConfig.setAccesslog(accesslog);
		if(!StringUtils.isEmpty(provideFilter)) {
			providerConfig.setFilter(provideFilter);
		}
		return providerConfig;
	}
	
	@Bean
	public ConsumerConfig productConsumerConfig() {
		ConsumerConfig consumerConfig=new ConsumerConfig();
		consumerConfig.setTimeout(5000);
		consumerConfig.setCheck(false);
		consumerConfig.setRetries(0);
		return consumerConfig;
	}
	
}
